package com.epam.training;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.epam.training.domain.Penalty;
import com.epam.training.service.penalty.PenaltyService;

@Controller
public class AddPenaltyController {
	private final PenaltyService service;

	@Autowired
	public AddPenaltyController(PenaltyService service) {
		this.service = service;
	}

	@ModelAttribute
	private Penalty createBonus(@RequestParam BigDecimal pAmount,
			@RequestParam String pName, @RequestParam String pDescription) {
		Penalty penalty = new Penalty();
		penalty.setAmount(pAmount);
		penalty.setName(pName);
		penalty.setDescription(pDescription);

		return penalty;
	}

	@RequestMapping(value = "/admin/addPenalty", method = RequestMethod.POST)
	private String addNewPenalty(@ModelAttribute Penalty penalty, Model model) {
		service.save(penalty);

		model.addAttribute("message", "penalty.added");

		return AdminPageController.ADMIN_PAGE;
	}

	@RequestMapping(value = "/admin/updatePenalty", method = RequestMethod.POST)
	private String addNewPenalty(@ModelAttribute Penalty penalty,
			@RequestParam Integer penaltyId, Model model) {
		service.update(penalty, penaltyId);

		model.addAttribute("message", "penalty.update");

		return AdminPageController.ADMIN_PAGE;
	}
}
